package features.advance.leetcode.interview.offer.medium.fsm;

/**
 * @author LIN
 * @date 2021-05-18
 */
public class FsmDemo {

    private StateEnum state = StateEnum.OFF;

    private void init(){
        if (state == null){
            state = StateEnum.OFF;
        }
    }

    public void turnOff(){
        init();
        if (StateEnum.ON == state) {
            state = StateEnum.OFF;
        }
    }

    public void turnOn(){
        init();
        if (StateEnum.OFF == state) {
            state = StateEnum.ON;
        }
    }

    public boolean isOn(){
        return state != null && state == StateEnum.ON;
    }

    public boolean isOff(){
        return !isOn();
    }

    public static void main(String[] args) {
        FsmDemo fsmDemo = new FsmDemo();
        boolean on = fsmDemo.isOn();
        System.out.println(on);
        fsmDemo.turnOn();
        System.out.println(fsmDemo.isOn());
        fsmDemo.turnOff();
        System.out.println(fsmDemo.isOn());
        System.out.println(fsmDemo.isOff());

    }

    /**
     * @author LIN
     * @date 2021-5-18
     */
    enum  StateEnum {
        OFF,
        ON;
    }
}
